/**
 * @author: zouye<yzyngz@foxmail.com>
 * @date: 2022-01-09 17:01:19
 * @copyright: zouye<yzyngz@foxmail.com>
 */
package com.yeah.voice.recognition.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yeah.voice.recognition.common.Properties;
import com.yeah.voice.recognition.common.Result;
import com.yeah.voice.recognition.entity.TbMarkPeople;
import com.yeah.voice.recognition.service.VoiceService;

/**
 * 用户登录操作
 */
@RestController
@RequestMapping("user/")
public class UserController {
	
	@Autowired
	private Properties properties;

	@Autowired
	private VoiceService voiceService;
	
	/**
	 * 用户登录，检查是否还能继续答题
	 * @param people
	 * @return
	 */
	@RequestMapping("login")
	public Result save(@RequestBody TbMarkPeople people) {
		if (!voiceService.checkCanAnswerByPhone(people.getPhone())) {
			return Result.fail(403, "每人限答 "+ properties.getPeopleAnswerCount() +" 次，您已作答过超过最高限制，不能在作答了，十分感谢您的支持~");
		}
		
		try {
			voiceService.saveUser(people);
			
			return Result.ok(people);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(500, e.getMessage());
		}
	}
	
	/**
	 * 更新用户的个人信息
	 * @param people
	 * @return
	 */
	@RequestMapping("updateInfo")
	public Result updateInfo(@RequestBody TbMarkPeople people) {
		try {
//			voiceService.saveUser(people);
			voiceService.updateUserInfo(people);

			return Result.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(500, e.getMessage());
		}
	}
}
